package com.example.protestgps;

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class VoiceMsg extends Activity {

	MediaRecorder recorder;
	private File mOutputFile;
	Button recordButton;
	Button stopButton;
	int click = 0;
	int id = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.voicerecord);
		initialButton();
		recordButton.setOnClickListener(startListerner);
		stopButton.setOnClickListener(stopListerner);
		recorder = new MediaRecorder();

	}

	private void initialButton() {
		// TODO Auto-generated method stub
		recordButton = (Button) findViewById(R.id.btnRecord);
		stopButton = (Button) findViewById(R.id.butnStop);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (recorder != null) {
			recorder.release();
			recorder = null;
		}
	}

	private View.OnClickListener startListerner = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
						
			mOutputFile = new File("/storage/extSdCard/source.mp3");
			Log.d("mOutputFile1",""+mOutputFile);
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			recorder.setOutputFile(mOutputFile.getAbsolutePath());
			
			try {
				recorder.prepare();
				
			} catch (Exception e) {
				e.printStackTrace();
			}

			recorder.start();
			recordButton.setEnabled(false);
			stopButton.setEnabled(true);

		}

	};



	private View.OnClickListener stopListerner = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			recorder.stop();
			//recordButton.setEnabled(true);
			//stopButton.setEnabled(false);
			Intent gotoUploadFile = new Intent(getApplicationContext(),UploadFile.class);
			gotoUploadFile.putExtra("Path", mOutputFile.toString());
			startActivity(gotoUploadFile);
			
			
		}
	};

}
